package com.dimenlink.web.controller;

import com.dimenlink.entity.dto.TokenUserInfoDto;
import com.dimenlink.entity.enums.ResponseCodeEnum;
import com.dimenlink.entity.enums.UserActionTypeEnum;
import com.dimenlink.entity.enums.VideoRecommendTypeEnum;
import com.dimenlink.entity.po.UserAction;
import com.dimenlink.entity.po.VideoInfo;
import com.dimenlink.entity.po.VideoInfoFile;
import com.dimenlink.entity.query.UserActionQuery;
import com.dimenlink.entity.query.VideoInfoFileQuery;
import com.dimenlink.entity.query.VideoInfoQuery;
import com.dimenlink.entity.vo.PaginationResultVO;
import com.dimenlink.entity.vo.ResponseVO;
import com.dimenlink.entity.vo.VideoInfoResultVO;
import com.dimenlink.exception.BusinessException;
import com.dimenlink.service.UserActionService;
import com.dimenlink.service.VideoInfoFileService;
import com.dimenlink.service.VideoInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/video")
@Validated
@Slf4j
public class VideoController extends ABaseController{

    @Resource
    private VideoInfoService videoInfoService;
    @Resource
    private VideoInfoFileService videoInfoFileService;
    @Resource
    private UserActionService userActionService;

    @RequestMapping("/loadRecommendVideo")
    public ResponseVO loadRecommendVideo() {
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        videoInfoQuery.setQueryUserInfo(true);
        videoInfoQuery.setOrderBy("create_time desc");
        videoInfoQuery.setRecommendType(VideoRecommendTypeEnum.RECOMMEND.getType());
        List<VideoInfo> recommendVideoList = videoInfoService.findListByParam(videoInfoQuery);
        return getSuccessResponseVO(recommendVideoList);
    }

    @RequestMapping("/loadVideo")
    public ResponseVO loadVideo(Integer pCategoryId, Integer categoryId, Integer pageNo) {
        // 创建 VideoInfoQuery 对象，用于封装视频信息查询的条件
        VideoInfoQuery videoInfoQuery = new VideoInfoQuery();
        // 设置视频分类 ID，用于按分类筛选视频
        videoInfoQuery.setCategoryId(categoryId);
        // 设置父级视频分类 ID
        videoInfoQuery.setpCategoryId(pCategoryId);
        // 设置分页页码，指定要获取第几页的视频数据
        videoInfoQuery.setPageNo(pageNo);
        // 设置是否查询用户信息，true 表示在查询视频的同时关联查询发布视频的用户信息
        videoInfoQuery.setQueryUserInfo(true);
        // 设置排序规则，按创建时间（create_time）降序（desc）排列，即最新创建的视频排在前面
        videoInfoQuery.setOrderBy("create_time desc");
        // 设置推荐类型为“不推荐”，通过枚举 VideoRecommendTypeEnum 获取对应类型值，用于筛选非推荐视频
        videoInfoQuery.setRecommendType(VideoRecommendTypeEnum.NO_RECOMMEND.getType());

        // 调用 videoInfoService 的分页查询方法，传入查询条件，获取分页结果
        PaginationResultVO resultVO = videoInfoService.findListByPage(videoInfoQuery);

        return getSuccessResponseVO(resultVO);
    }

    @RequestMapping("/getVideoInfo")
    public ResponseVO getVideoInfo(@NotEmpty String videoId) {
        // 调用 videoInfoService 的方法，根据 videoId 查询视频信息
        VideoInfo videoInfo = videoInfoService.getVideoInfoByVideoId(videoId);

        // 校验查询结果：若 videoInfo 为 null，说明未查到对应视频
        if (videoInfo == null) {
            throw new BusinessException(ResponseCodeEnum.CODE_404);
        }

        // 获取用户信息行为
        TokenUserInfoDto userInfoDto = getTokenUserInfoDto();
        List<UserAction> userActionList = new ArrayList<>();
        if (userInfoDto != null) {
            UserActionQuery actionQuery = new UserActionQuery();
            actionQuery.setVideoId(videoId);
            actionQuery.setUserId(userInfoDto.getUserId());
            actionQuery.setActionTypeArray(new Integer[]{UserActionTypeEnum.VIDEO_LIKE.getType(),
                    UserActionTypeEnum.VIDEO_COLLECT.getType(),
                    UserActionTypeEnum.VIDEO_COIN.getType()});
            userActionList = userActionService.findListByParam(actionQuery);
        }

        // 将查询到的 VideoInfo 转换为 VideoInfoResultVO
        VideoInfoResultVO resultVO = new VideoInfoResultVO(videoInfo, userActionList);
        // 封装成功响应
        return getSuccessResponseVO(resultVO);
    }

    @RequestMapping("/loadVideoPList")
    public ResponseVO loadVideoPList(@NotEmpty String videoId) {
        // 1. 构建查询条件对象
        VideoInfoFileQuery videoInfoFileQuery = new VideoInfoFileQuery();

        // 2. 设置查询条件：关联的视频 ID
        videoInfoFileQuery.setVideoId(videoId);

        // 3. 设置排序规则：按 file_index 字段「升序」排列
        videoInfoFileQuery.setOrderBy("file_index asc");

        // 4. 调用 Service 层方法，根据条件查询视频片段列表
        List<VideoInfoFile> fileList = videoInfoFileService.findListByParam(videoInfoFileQuery);

        // 5. 封装结果并返回
        return getSuccessResponseVO(fileList);
    }
}
